package Network;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import BlindCounters.DoubleBlindCounter;
import DataStructures.PokerTable;

public class Server {

	private int serverPort;
	private ServerSocket serv;
	private ConnectionAcceptThread connThread;
	private NetworkHoldemDealer dealer;
	
	public Server(int serverPort, NetworkHoldemDealer dealer){
		this.serverPort = serverPort;
		this.dealer = dealer;
	}
	
	public boolean start(){
			
		boolean successful = false;
		
		// create the socket server
		try {
			serv = new ServerSocket(serverPort);
			successful = true;
		} catch (IOException e) {
			e.printStackTrace();
		}
		if(successful){
			connThread = new ConnectionAcceptThread(serv, dealer);
			connThread.start();
		}
		return successful;
	}
	
	
	protected class ConnectionAcceptThread extends Thread {
		
		ServerSocket serv;
		
		public ConnectionAcceptThread(ServerSocket serv, NetworkHoldemDealer dealer){
			this.serv = serv;
		}
		
		public void run(){
			Socket client = null;
			IncomingPlayer p = null;
			while(true){
				try {
					client = serv.accept();
					p = new IncomingPlayer(client, dealer);
					p.start();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}		
}
